﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class ConsumeSolutionForm : Form
    {
        public ConsumeSolutionForm()
        {
            InitializeComponent();
            this.Opacity = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        private void ConsumeSolutionForm_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            ZG_member.BLL.ConsProcManager sm=new ZG_member.BLL.ConsProcManager();
            
            this.dgvConsumeSolution.DataSource = sm.GtList();
            consumeSolution con = new consumeSolution();
            ZG_member.Model.consumeSolution conMode = new ZG_member.Model.consumeSolution();
            DataSet dt;
            foreach (DataGridViewRow item in dgvConsumeSolution.Rows)
            {
                if (item.Cells["nextLevelIdNew"].Value != "")
                {
                    int nextId = Convert.ToInt32(item.Cells["nextLevelIdNew"].Value);
                    if (nextId != 0)
                    {
                        dt = con.GetList("id=" + nextId);
                        item.Cells["nextLevelIdNew"].Value = dt.Tables[0].Rows[0]["solutionName"];
                    }
                    else {
                        item.Cells["nextLevelIdNew"].Value = "最高";
                    }
                }
                if (item.Cells["cuxiaozhekou"].Value != null)
                {
                    if (Convert.ToBoolean(item.Cells["cuxiaozhekou"].Value) == true)
                    {
                        item.Cells["cuxiaozhekou1"].Value = "是";
                    }
                    else
                    {
                        item.Cells["cuxiaozhekou1"].Value = "否";
                    }
                }
            }
           
        }
        public void dgvConsumeSolutionBind()
        {

            //try
            //{
                //consumeSolution solution = new consumeSolution();
                //DataTable dt = solution.GetList("").Tables[0];
                ZG_member.BLL.ConsProcManager sm = new ZG_member.BLL.ConsProcManager();

                this.dgvConsumeSolution.DataSource = sm.GtList();

                //dgvConsumeSolution.DataSource = dt;
                dgvConsumeSolution.AllowUserToAddRows = false;
                consumeSolution con = new consumeSolution();
                ZG_member.Model.consumeSolution conMode = new ZG_member.Model.consumeSolution();
                DataSet dt;
                foreach (DataGridViewRow item in dgvConsumeSolution.Rows)
                {
                    if (item.Cells["nextLevelIdNew"].Value != "")
                    {
                        int nextId = Convert.ToInt32(item.Cells["nextLevelIdNew"].Value);
                        if (nextId != 0)
                        {
                            dt = con.GetList("id=" + nextId);
                            item.Cells["nextLevelIdNew"].Value = dt.Tables[0].Rows[0]["solutionName"];
                        }
                        else
                        {
                            item.Cells["nextLevelIdNew"].Value = "最高";
                        }

                    }
                    if (item.Cells["cuxiaozhekou"].Value != null)
                    {
                        if (Convert.ToBoolean(item.Cells["cuxiaozhekou"].Value) == true)
                        {
                            item.Cells["cuxiaozhekou1"].Value = "是";
                        }
                        else
                        {
                            item.Cells["cuxiaozhekou1"].Value = "否";
                        }
                    }
                }
            //    foreach (DataGridViewRow dgv in dgvConsumeSolution.Rows)
            //    {
            //        if (dgv.Cells["nextLevelId"].Value != null)
            //        {
            //            consumeSolution solu = new consumeSolution();
            //            Model.consumeSolution soluModel = new Model.consumeSolution();
            //            soluModel = solu.GetModelList("id='" + dgv.Cells["nextLevelId"].Value + "'")[0];
            //            dgv.Cells["nextLevelIdNew"].Value = soluModel.solutionName;
            //        }
            //        if (dgv.Cells["cardTemplateId"].Value != null)
            //        {
            //            cardTemplates card = new cardTemplates();
            //            Model.cardTemplates cardModel = new Model.cardTemplates();
            //            cardModel = card.GetModelList("id='" + dgv.Cells["cardTemplateId"].Value + "'")[0];
            //            dgv.Cells["cardTemplateIdNew"].Value = cardModel.name;
            //        }

            //    }
            //}
            //catch (Exception e)
            //{
            //    MessageBox.Show(e.ToString());
            //}
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            ID = 0;
            ConsumeSolutionSettingForm setting = new ConsumeSolutionSettingForm(this);
            setting.ShowDialog();
  
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            consumeSolution solution = new consumeSolution();
            Model.consumeSolution kaid = solution.GetModel(int.Parse(dgvConsumeSolution.CurrentRow.Cells["IDS"].Value.ToString()));
            DataSet ds = solution.GetList(" nextlevelid = " + kaid.id);
            if (ds.Tables[0].Rows.Count <= 0)
            {
                DialogResult dr = MessageBox.Show("此信息正在使用删除会导致数据错误\r\n您确定要删除该条信息吗？", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (dr == DialogResult.OK)
                {
                    int ID = int.Parse(dgvConsumeSolution.CurrentRow.Cells["IDS"].Value.ToString());
                    
                    memberInfo member = new memberInfo();
                    DataTable dt= member.GetList(" cardType=" + ID + "").Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        MessageBox.Show("删除失败,会员中有此等级的会员,请先更改该会员的等级");
                        return;
                    }

                    if (solution.Delete(ID))
                    {
                        dgvConsumeSolutionBind();
                    }
                }
            }
            else
            {
                MessageBox.Show("此信息正在使用删除会导致数据错误！请您逐级删除！");
            }
        }
        public int ID;
        private void btnupt_Click(object sender, EventArgs e)
        {
            if (dgvConsumeSolution.Rows.Count > 0)
            {
                ID = int.Parse(dgvConsumeSolution.CurrentRow.Cells["IDS"].Value.ToString());
                ConsumeSolutionSettingForm setting = new ConsumeSolutionSettingForm(this);

                if (dgvConsumeSolution.CurrentRow.Cells["solutionName"].Value != null)
                {
                    setting.methodNameTxt.Text = dgvConsumeSolution.CurrentRow.Cells["solutionName"].Value.ToString();
                }
                if (dgvConsumeSolution.CurrentRow.Cells["cardTemplateIdNew"].Value != null)
                {
                    //setting.cardStyleTemplateCbx.DropDownStyle = ComboBoxStyle.DropDown;
                    //setting.cardStyleTemplateCbx.Text = dgvConsumeSolution.CurrentRow.Cells["cardTemplateIdNew"].Value.ToString();
                    //setting.cardStyleTemplateCbx.DropDownStyle = ComboBoxStyle.DropDownList;
                    string names = dgvConsumeSolution.CurrentRow.Cells["cardTemplateIdNew"].Value.ToString();
                    //cardTemplates temp = new cardTemplates();
                    //Model.cardTemplates tempMode = new Model.cardTemplates();
                    //List<Model.cardTemplates> list= temp.GetModelList("name='"+names+"'");
                    //ConsumeSolutionSettingForm.TempName = list[0].id;
                    ConsumeSolutionSettingForm.TempName = names;

                }
                if (dgvConsumeSolution.CurrentRow.Cells["discountSolution"].Value != null)
                {
                    setting.disconntMethodTxt.Text = dgvConsumeSolution.CurrentRow.Cells["discountSolution"].Value.ToString();
                }
                if (dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value != null)
                {
                    string nextName = dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value.ToString();
                    if (nextName != "最高")
                    {
                        //consumeSolution com = new consumeSolution();
                        //Model.consumeSolution comMode = new Model.consumeSolution();
                        //List<Model.consumeSolution> list = com.GetModelList("solutionName='" + nextName + "'");
                        //ConsumeSolutionSettingForm.Values = list[0].id;
                        ConsumeSolutionSettingForm.Values = nextName;

                    }
                    else
                    {
                        ConsumeSolutionSettingForm.Values = "0";
                    }
                    //setting.cbxnextlevelid.Text = dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value.ToString();
                    ////setting.cbxnextlevelid.Text = dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value.ToString();
                    //if (dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value != "" && dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value == "已是最高等级")
                    //{
                    //    setting.cbxnextlevelid.Text = "已是最高等级";
                    //}
                }
                if (dgvConsumeSolution.CurrentRow.Cells["integralday"].Value != null)
                {
                    setting.textBox1.Text = dgvConsumeSolution.CurrentRow.Cells["integralday"].Value.ToString();
                }
                if (dgvConsumeSolution.CurrentRow.Cells["levelUp"].Value != null)
                {
                    setting.txtleaveUp.Text = dgvConsumeSolution.CurrentRow.Cells["levelUp"].Value.ToString();
                }
                if (dgvConsumeSolution.CurrentRow.Cells["consumeSolution1"].Value != null)
                {
                    //setting.saleBox.Items.Add(dgvConsumeSolution.CurrentRow.Cells["consumeSolution1"].Value.ToString());
                    string Solu = dgvConsumeSolution.CurrentRow.Cells["consumeSolution1"].Value.ToString();
                    string SoluName = Solu.Replace("|", "#");
                    string[] SoluNew = SoluName.Split('#');
                    foreach (var item in SoluNew)
                    {
                        if (item != "")
                        {
                            setting.saleBox.Items.Add(item);
                        }
                        else
                        {
                            break;
                        }
                    }

                }
                if (dgvConsumeSolution.CurrentRow.Cells["savingSolution"].Value != null)
                {
                    string Solu = dgvConsumeSolution.CurrentRow.Cells["savingSolution"].Value.ToString();
                    string SoluName = Solu.Replace("|", "#");
                    string[] SoluNew = SoluName.Split('#');
                    foreach (var item in SoluNew)
                    {
                        if (item != "")
                        {
                            setting.savingBox.Items.Add(item);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if (dgvConsumeSolution.CurrentRow.Cells["cuxiaozhekou1"].Value != null)
                {
                    if (dgvConsumeSolution.CurrentRow.Cells["cuxiaozhekou1"].Value == "是")
                    {
                        setting.radioButton1.Checked = true;
                    }
                    else
                    {
                        setting.radioButton2.Checked = true;
                    }
                }
                if (dgvConsumeSolution.CurrentRow.Cells["leaveDate"].Value != null)
                {
                    string leaveDate=dgvConsumeSolution.CurrentRow.Cells["leaveDate"].Value.ToString();
                    setting.numericUpDown1.Value = Convert.ToInt32(leaveDate.Substring(0, leaveDate.IndexOf('-') - 1));
                    setting.numericUpDown2.Value = Convert.ToInt32(leaveDate.Substring(leaveDate.IndexOf('-') + 1));
                }
                setting.ShowDialog();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void dgvConsumeSolution_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dgvConsumeSolution.Rows.Count > 0)
            {
                ID = int.Parse(dgvConsumeSolution.CurrentRow.Cells["IDS"].Value.ToString());
                ConsumeSolutionSettingForm setting = new ConsumeSolutionSettingForm(this);

                if (dgvConsumeSolution.CurrentRow.Cells["solutionName"].Value != null)
                {
                    setting.methodNameTxt.Text = dgvConsumeSolution.CurrentRow.Cells["solutionName"].Value.ToString();
                }
                if (dgvConsumeSolution.CurrentRow.Cells["cardTemplateIdNew"].Value != null)
                {
                    //setting.cardStyleTemplateCbx.DropDownStyle = ComboBoxStyle.DropDown;
                    //setting.cardStyleTemplateCbx.Text = dgvConsumeSolution.CurrentRow.Cells["cardTemplateIdNew"].Value.ToString();
                    //setting.cardStyleTemplateCbx.DropDownStyle = ComboBoxStyle.DropDownList;
                    string names = dgvConsumeSolution.CurrentRow.Cells["cardTemplateIdNew"].Value.ToString();
                    //cardTemplates temp = new cardTemplates();
                    //Model.cardTemplates tempMode = new Model.cardTemplates();
                    //List<Model.cardTemplates> list= temp.GetModelList("name='"+names+"'");
                    //ConsumeSolutionSettingForm.TempName = list[0].id;
                    ConsumeSolutionSettingForm.TempName = names;

                }
                if (dgvConsumeSolution.CurrentRow.Cells["discountSolution"].Value != null)
                {
                    setting.disconntMethodTxt.Text = dgvConsumeSolution.CurrentRow.Cells["discountSolution"].Value.ToString();
                }
                if (dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value != null)
                {
                    string nextName = dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value.ToString();
                    if (nextName != "最高")
                    {
                        //consumeSolution com = new consumeSolution();
                        //Model.consumeSolution comMode = new Model.consumeSolution();
                        //List<Model.consumeSolution> list = com.GetModelList("solutionName='" + nextName + "'");
                        //ConsumeSolutionSettingForm.Values = list[0].id;
                        ConsumeSolutionSettingForm.Values = nextName;

                    }
                    else
                    {
                        ConsumeSolutionSettingForm.Values = "0";
                    }
                    //setting.cbxnextlevelid.Text = dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value.ToString();
                    ////setting.cbxnextlevelid.Text = dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value.ToString();
                    //if (dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value != "" && dgvConsumeSolution.CurrentRow.Cells["nextLevelIdNew"].Value == "已是最高等级")
                    //{
                    //    setting.cbxnextlevelid.Text = "已是最高等级";
                    //}
                }
                if (dgvConsumeSolution.CurrentRow.Cells["integralday"].Value != null)
                {
                    setting.textBox1.Text = dgvConsumeSolution.CurrentRow.Cells["integralday"].Value.ToString();
                }
                if (dgvConsumeSolution.CurrentRow.Cells["levelUp"].Value != null)
                {
                    setting.txtleaveUp.Text = dgvConsumeSolution.CurrentRow.Cells["levelUp"].Value.ToString();
                }
                if (dgvConsumeSolution.CurrentRow.Cells["consumeSolution1"].Value != null)
                {
                    //setting.saleBox.Items.Add(dgvConsumeSolution.CurrentRow.Cells["consumeSolution1"].Value.ToString());
                    string Solu = dgvConsumeSolution.CurrentRow.Cells["consumeSolution1"].Value.ToString();
                    string SoluName = Solu.Replace("|", "#");
                    string[] SoluNew = SoluName.Split('#');
                    foreach (var item in SoluNew)
                    {
                        if (item != "")
                        {
                            setting.saleBox.Items.Add(item);
                        }
                        else
                        {
                            break;
                        }
                    }

                }
                if (dgvConsumeSolution.CurrentRow.Cells["savingSolution"].Value != null)
                {
                    string Solu = dgvConsumeSolution.CurrentRow.Cells["savingSolution"].Value.ToString();
                    string SoluName = Solu.Replace("|", "#");
                    string[] SoluNew = SoluName.Split('#');
                    foreach (var item in SoluNew)
                    {
                        if (item != "")
                        {
                            setting.savingBox.Items.Add(item);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if (dgvConsumeSolution.CurrentRow.Cells["cuxiaozhekou1"].Value != null)
                {
                    if (dgvConsumeSolution.CurrentRow.Cells["cuxiaozhekou1"].Value == "是")
                    {
                        setting.radioButton1.Checked = true;
                    }
                    else
                    {
                        setting.radioButton2.Checked = true;
                    }
                }
                if (dgvConsumeSolution.CurrentRow.Cells["leaveDate"].Value != null && dgvConsumeSolution.CurrentRow.Cells["leaveDate"].Value != "")
                {
                    string leaveDate = dgvConsumeSolution.CurrentRow.Cells["leaveDate"].Value.ToString();
                    setting.numericUpDown1.Value = Convert.ToInt32(leaveDate.Substring(0, leaveDate.IndexOf('-')));
                    setting.numericUpDown2.Value = Convert.ToInt32(leaveDate.Substring(leaveDate.IndexOf('-') + 1));
                }
                setting.ShowDialog();
            }
        }
        int flag = 0;
        private void ConsumeSolutionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

       

        



       
    }
}